<?php
function sortByOneKey(array $array, $key, $asc = true) {
     $result = array();
         
     $values = array();
     foreach ($array as $id => $value) {
         $values[$id] = isset($value[$key]) ? $value[$key] : '';
     }
         
     if ($asc) {
         asort($values);
     }
     else {
         arsort($values);
     }
         
     foreach ($values as $key => $value) {
         $result[$key] = $array[$key];
     }
         
     return $result;
 }



// Consider the following example:



 $users = array(
     1 => array('name' => 'John', 'age' => 35),
     2 => array('name' => 'Alice', 'age' => 23),
     3 => array('name' => 'Bob', 'age' => 26)
 );
         
$sortedByNameAsc = sortByOneKey($users, 'name');
$sortedByNameDesc = sortByOneKey($users, 'name', false);
         
 echo "Sorted by Name in ascending order: ";
 echo "<pre>" . print_r($sortedByNameAsc, true) . "</pre>";
 echo "<br /><br />Sorted by Name in descending order: ";
 echo "<pre>" . print_r($sortedByNameDesc, true) . "</pre>";



 //obj

function sortByOneKey(array $array, $key, $asc = true) {
     $result = array();
         
     $values = array();
     foreach ($array as $id => $value) {
         $values[$id] = $value->$key; #isset($value->$key) ? $value->$key : '';
     }

     if ($asc) {
         asort($values);
     }
     else {
         arsort($values);
     }


     foreach ($values as $key => $value) {
         $result[$key] = $array[$key];
     }

     return $result;
 }


// procedural: 
uasort($collection, 'my_sort_function'); 

// Object Oriented 
uasort($collection, array($this, 'mySortMethod')); 

// Objet Oriented with static method 
uasort($collection, array('self', 'myStaticSortMethod'));  

function sortByOneKey(array $array, $key, $asc = true)
{
    $result = array();

    $values = array();
    foreach ($array as $id => $value)
    {
        if($value->$key)
            $values[$id] = $value->$key; #isset($value->$key) ? $value->$key : '';
        else
            if($asc)
                $values[$id] = PHP_INT_MAX ;
            else
                $values[$id] = -1;
    }

    if ($asc)
    {
        asort($values);
    }
    else
    {
        arsort($values);
    }


    foreach ($values as $key => $value)
    {
        $result[$key] = $array[$key];
    }

    return $result;
}


function multiSort()
{
        //get args of the function 
        $args = func_get_args();
        $c    = count($args);
        if ($c < 2) {return false;}
        //get the array to sort 
        $array = array_splice($args, 0, 1);
        $array = $array[0];
        //sort with an anoymous function using args 
        usort($array, function($a, $b) use($args) {

                    $i   = 0;
                    $c   = count($args);
                    $cmp = 0;
                    while ($cmp == 0 && $i < $c)
                    {
                        $cmp = ($a->$args[$i] - $b->$args[$i]) ?
                                ($a->$args[$i] - $b->$args[$i]) /
                                abs($a->$args[$i] - $b->$args[$i]) : 0;

                        $dir = $args[$i + 1];
                        // Sort null value to end
                        $cmp = $dir == SORT_ASC && $a->$args[$i] ? $cmp : -$cmp;
                        $i   = $i + 2;
                    }

                    return $cmp;
                });

        return $array;
    }












?> 

